if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_HangTon_InsertTonKhoOfNhapHangByMaChiNhanh_MaHang]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[sp_HangTon_InsertTonKhoOfNhapHangByMaChiNhanh_MaHang]
GO
CREATE PROC [sp_HangTon_InsertTonKhoOfNhapHangByMaChiNhanh_MaHang]
	@Ma_hang					int,
	@So_luong					float,
	@Ghi_chu					nvarchar(100),
	@Gia_nhap_cuoi				float,
	@Gia_xuat_cuoi				float,
	@Ma_chi_nhanh				int,
	@OutID						int OUTPUT
AS
BEGIN
	DECLARE @Ngay_cap_nhat_cuoi datetime
	DECLARE @Ma_kho_hang int
	DECLARE @Ma_loai_hang_ton int
	
	SET @Ngay_cap_nhat_cuoi = getdate()
	SELECT @Ma_kho_hang = ID FROM KhoHang WHERE Ma_chi_nhanh = @Ma_chi_nhanh
	--Ma loai hang ton : loai binh thuong
	SET @Ma_loai_hang_ton = 0	
	INSERT INTO HangTon(
					Ma_hang,
					So_luong,
					Ma_loai_hang_ton,
					Ghi_chu,
					Gia_nhap_cuoi,
					Gia_xuat_cuoi,
					Ma_kho_hang,
					Ngay_cap_nhat_cuoi
				)
	VALUES(				
		@Ma_hang,
		@So_luong,
		@Ma_loai_hang_ton,
		@Ghi_chu,
		@Gia_nhap_cuoi,
		@Gia_xuat_cuoi,
		@Ma_kho_hang,
		@Ngay_cap_nhat_cuoi
	)
	SELECT @OutID = SCOPE_IDENTITY()
END
